﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZF.BLL;
using System.Linq.Dynamic;
using System.Linq.Expressions;
using ZF.BLL.Linq;
using ZF.BLL;
using GCELibrary.Current;
namespace ZF.WS.UserControls.Index
{
    public partial class NewHouse : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {



            List<object> sourceList = null;

            var cacheKey = "_Index_NewHouse";
            var obj = HttpCache.GetCache(cacheKey);
            if (obj != null)
            {
                sourceList = obj as List<object>;
            }
            else
            {
                sourceList = new List<object>();
                using (var dc = SiteInfo.CreateDBDataContext())
                {

                    house.Search s = new ZF.WS.house.Search();
                    var gzIdArr =s.GZCity.Select(p => p.AreaId).ToArray();
                    var cz = dc.H_House.Where(p => p.Status == 1 && p.IsVerify).Where(p => p.DealType == 1 || p.DealType == 2)
                        .Where(p=>gzIdArr.Contains(p.AreaId1))
                         .OrderByDescending(p=>p.AddTime)
                         .Take(10).ToList().Select(p =>
                             new
                             {
                                 Address = p.Address,
                                 House = p,
                                 City = SiteInfo.Instance.GetAreaOrDefault(p.AreaId1).Name,
                                 Area = p.AreaId1 != p.AreaId2 ? DictionaryConfig.HArea.DefaultIfEmpty(a => p.AreaId2.HasValue && a.AreaId == p.AreaId2.Value, new ZF.Model.Dic_Area()).Name : "",
                                 Model = string.Format("{0}房{1}厅", p.HX_Fan, p.HX_Tin),
                                 Price = p.Price,
                                 AddTime = p.AddTime,
                             });
                    sourceList.Add(cz);






                    var qz = dc.H_Qiuzu.Where(p => p.Status == 1 && p.IsVerify).Where(p => p.DealType == 1)
                         .OrderByDescending(p => p.AddTime).Take(10).ToList().Select(p =>
                             new
                             {

                                 House = p,
                                 City = SiteInfo.Instance.GetAreaOrDefault(p.AreaId1).Name,
                                 Area = p.AreaId1 != p.AreaId2?DictionaryConfig.HArea.DefaultIfEmpty(a => p.AreaId2.HasValue && a.AreaId == p.AreaId2.Value, new ZF.Model.Dic_Area()).Name:"",
                                 Model = string.Format("{0}房{1}厅", p.HX_Fan, p.HX_Tin),
                                 Price = p.Price,
                                 AddTime = p.AddTime,
                             });
                    sourceList.Add(qz);



                    var es = dc.H_House.Where(p => p.Status == 1 && p.IsVerify).Where(p => p.DealType == 3)
                        .Where(p => gzIdArr.Contains(p.AreaId1))
                        .OrderByDescending(p => p.AddTime).Take(10).ToList().Select(p =>
                            new
                            {
                                Address = p.Address,
                                House = p,
                                City = SiteInfo.Instance.GetAreaOrDefault(p.AreaId1).Name,
                                Area = p.AreaId1 != p.AreaId2 ? DictionaryConfig.HArea.DefaultIfEmpty(a => p.AreaId2.HasValue && a.AreaId == p.AreaId2.Value, new ZF.Model.Dic_Area()).Name : "",
                                Model = string.Format("{0}房{1}厅", p.HX_Fan, p.HX_Tin),
                                Price = p.Price,
                                AddTime = p.AddTime,
                            });
                    sourceList.Add(es);




                    var qs = dc.H_Qiuzu.Where(p => p.Status == 1 && p.IsVerify).Where(p => p.DealType == 2)
                               .OrderByDescending(p => p.AddTime).Take(10).ToList().Select(p =>
                             new
                             {

                                 House = p,
                                 City = SiteInfo.Instance.GetAreaOrDefault(p.AreaId1).Name,
                                 Area = p.AreaId1 != p.AreaId2 ? DictionaryConfig.HArea.DefaultIfEmpty(a => p.AreaId2.HasValue && a.AreaId == p.AreaId2.Value, new ZF.Model.Dic_Area()).Name : "",
                                 Model = string.Format("{0}房{1}厅", p.HX_Fan, p.HX_Tin),
                                 Price = p.Price,
                                 AddTime = p.AddTime,
                             });
                    sourceList.Add(qs);


                    SiteInfo.Instance.SetPageCache(cacheKey,sourceList);



                }
            
            }
            this.lvNewCZ.DataSource = sourceList[0];
            this.lvNewCZ.DataBind();
            this.lvNewQZ.DataSource = sourceList[1];
            this.lvNewQZ.DataBind();
            this.lvNewESSF.DataSource = sourceList[2];
            this.lvNewESSF.DataBind();

            this.lvNewESQG.DataSource = sourceList[3];
            this.lvNewESQG.DataBind();
        }
    }
}